package com.zyf.chapter02.creation.singleton;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 线程内唯一的 Id 生成器
 * @author yifeng
 */
public class IdGeneratorThreadOnly {
    private AtomicLong id = new AtomicLong(0);

    private static final ConcurrentHashMap<Long, IdGeneratorThreadOnly> instances
            = new ConcurrentHashMap<>();

    private IdGeneratorThreadOnly() {}

    public static IdGeneratorThreadOnly getInstance() {
        Long currentThreadId = Thread.currentThread().getId();
        instances.putIfAbsent(currentThreadId, new IdGeneratorThreadOnly());
        return instances.get(currentThreadId);
    }

    public long getId() {
        return id.incrementAndGet();
    }
}
